﻿using System;
using System.Collections.Generic;
using Medianamik.Core;
using Medianamik.Core.DAL;
using Medianamik.Core.DAL.Repositories;
using SubSonic;
using Medianamik.Core.DAL.Services;
using System.Linq;

namespace Medianamik.UI.Web.Shop.Data.Repositories
{
    public class ModelRepository<TCollection, TItem> : MedianamikRepository<TCollection, TItem>, IModelRepository<TItem>
        where TItem : Interfaces.IModel, IRecordBase, new()
        where TCollection : ICollectionWithChildren<TCollection, TItem>, new()
    {
        public ModelRepository(ICultureService cultureService, IContentStateService contentStateService, INodeMapper nodeMapper)
            : base(cultureService, contentStateService, nodeMapper)
        {
        }

        public virtual IList<TItem> GetModels(Guid? modelGroupId)
        {
            return modelGroupId != null ? ChildsOf(modelGroupId.Value, true, 1) : new List<TItem>();
        }

        public virtual IList<TItem> GetModelsByShoppingCartItem(Guid? shoppingCartItemId)
        {
            throw new NotImplementedException();
        }


        public virtual IList<TItem> GetModelsByParent(Guid parentNodeId, string cultureName, ContentState contentState, ActiveStatus activeStatus)
        {
            var models = new TCollection();

            models.Where(m => m.ParentNodeId == parentNodeId);
            models.Load(cultureName, contentState, activeStatus);

            return models;
        }
    }


    public class ModelRepository : ModelRepository<ModelCollection, Model>, IModelRepository
    {
        public ModelRepository(ICultureService cultureService, IContentStateService contentStateService, INodeMapper nodeMapper)
            : base(cultureService, contentStateService, nodeMapper)
        {
        }

        public override IList<Model> GetModelsByShoppingCartItem(Guid? shoppingCartItemId)
        {
            var generation = (int)ContentStateService.GetCurrentContentState();

            var query = new Select()
                .From(Model.Schema)
                .InnerJoin(ModelsInShoppingCartItems.Schema.TableName, ModelsInShoppingCartItems.Columns.Models, Model.Schema.TableName, Model.Columns.NodeId)
                .Where(ModelsInShoppingCartItems.Columns.ShoppingCartItems).IsEqualTo(shoppingCartItemId.ToString())
                .And(Model.Columns.Generation).IsEqualTo(generation)
                .And(Model.Columns.IsActive).IsEqualTo(true)
                .And(Model.Columns.CultureName).IsEqualTo(CultureService.GetCurrentCulture().Name)
                .And(ModelsInShoppingCartItems.Schema.GetColumn(ModelsInShoppingCartItems.Columns.Generation)).IsEqualTo(generation);

            return query.ExecuteAsCollection<ModelCollection>();
        }

    }

    public interface IModelRepository<TItem> : IMedianamikRepository<TItem>
        where TItem : Interfaces.IModel
    {
        IList<TItem> GetModels(Guid? modelGroupId);
        IList<TItem> GetModelsByShoppingCartItem(Guid? shoppingCartItemId);
        IList<TItem> GetModelsByParent(Guid parentNodeId, string cultureName, ContentState contentState, ActiveStatus activeStatus);
    }

    public interface IModelRepository : IModelRepository<Model>
    {
    }
}

